package com.wedu.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wedu.common.annotation.SysLog;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.common.validator.ValidatorUtils;
import com.wedu.modules.sys.entity.SysConfigEntity;
import com.wedu.modules.sys.entity.SysDictEntity;
import com.wedu.modules.sys.entity.SysTest;
import com.wedu.modules.sys.service.SysDictService;
import com.wedu.modules.sys.service.SysTestService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/test")
public class SysTestController extends AbstractController{

    @Autowired
    private SysTestService sysTestService;

    @Autowired
    private SysDictService sysDictService;

    @GetMapping("/list")
    @RequiresPermissions("sys:test:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysTestService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:test:info")
    public R info(@PathVariable("id") Long id){
        SysTest config = sysTestService.getById(id);
        return R.ok().put("config", config);
    }

//  保存
    @PostMapping("/save")
    @RequiresPermissions("sys:test:save")
    public R save(@RequestBody SysTest sysTest){
        ValidatorUtils.validateEntity(sysTest);
        sysTest.setCreateBy(getUserId());
        sysTest.setUpdateBy(getUserId());
        sysTestService.saveConfig(sysTest);
        return R.ok();
    }

//    @SysLog("删除配置")
    @PostMapping("/delete")
    @RequiresPermissions("sys:test:delete")
    public R delete(@RequestBody Long[] ids){
        sysTestService.deleteBatch(ids);

        return R.ok();
    }

//    @SysLog("修改配置")
    @PostMapping("/update")
    @RequiresPermissions("sys:test:update")
    public R update(@RequestBody SysTest sysTest){
        ValidatorUtils.validateEntity(sysTest);
        sysTest.setUpdateBy(getUserId());
        sysTestService.update(sysTest);
        return R.ok();
    }

    @GetMapping("/dictionary")
    @RequiresPermissions("sys:test:list")
    public R dictionary(@RequestParam Map<String, Object> params){
        //字典操作 下拉框
        List<Map<String,Object>> companyList = new ArrayList<>();
        //获取字典表中的公司字段
        QueryWrapper<SysDictEntity> companyType = new QueryWrapper<SysDictEntity>()
                .eq("type",2)
                .eq("code","company");
        //遍历获取所有公司信息
        List<SysDictEntity> companyTypeList = sysDictService.list(companyType);
        for (SysDictEntity sysDictEntity : companyTypeList) {
            Map<String,Object> companyMap = new HashMap<>();
            companyMap.put("value",sysDictEntity.getName());
            companyMap.put("label",sysDictEntity.getName());
            companyList.add(companyMap);
        }

        //字典（多选）
        List<Map<String,Object>> skillsList = new ArrayList<>();
        QueryWrapper<SysDictEntity> skillsType = new QueryWrapper<SysDictEntity>()
                .eq("type",2)
                .eq("code","skills");
        List<SysDictEntity> skillsTypeList = sysDictService.list(skillsType);
        for (SysDictEntity sysDictEntity : skillsTypeList) {
            Map<String,Object> skillsMap = new HashMap<>();
            skillsMap.put("label",sysDictEntity.getName());
            skillsMap.put("key",sysDictEntity.getName());
//            skillsMap.put("List",sysDictEntity.getName());
            skillsList.add(skillsMap);
        }
        R r= R.ok();
        r.put("companyList",companyList);
        r.put("skillsList",skillsList);
        return r;
    }

}
